#!/usr/bin/env bash

function install_termux() {
  echo '更新系统'
  termux-setup-storage
  apt list --upgradable
  yes Y | pkg upgrade
  yes Y | pkg install root-repo x11-repo openssh git curl wget bat croc zsh lsd neovim build-essential python nodejs fontconfig-utils rust fd ripgrep tur-repo fzf fish libxlst libxml2
  yes Y | pkg install code-server

  echo '安装packer.nvim'
  git clone --depth 1 https://github.com/wbthomason/packer.nvim ~/.local/share/nvim/site/pack/packer/start/packer.nvim

  echo '安装ZSH及相关主题插件'
  echo Y | sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  sed -i '/ZSH_THEME/s/robbyrussell/agnoster/' ~/.zshrc
  git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
  git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
  sed -i '2s/# //' .zshrc
  sed -i "s/robbyrussell/agnoster/;s/(git)/(git zsh-autosuggestions zsh-syntax-highlighting)/" .zshrc

  echo '安装Neovim'
  git clone https://github.com/LunarVim/Neovim-from-scratch.git ~/.config/nvim

  echo '安装字体'
  mkdir fonts && cd fonts && wget https://github.com/ryanoasis/nerd-fonts/releases/download/v2.3.3/3270.zip && unzip *.zip && wget https://github.com/microsoft/vscode-codicons/raw/main/dist/codicon.ttf && wget https://github.com/googlefonts/noto-emoji/blob/main/fonts/NotoColorEmoji.ttf && cd && mv fonts ~/.local/share/ && fc-cache -f -v
  pip install pynvim bs4 confuse yagmail tenacity && npm i -g neovim

  echo '安装LunarVim'
  curl https://raw.githubusercontent.com/lunarvim/lunarvim/fc6873809934917b470bff1b072171879899a36b/utils/installer/install.sh >~/install.sh && sed -i 's/sudo apt/pkg/g' ~/install.sh && sed -i '366a \\tchmod -R 0777 "$src.old/"' ~/install.sh && sed -i '370a \\tchmod -R 0777 "$src.old/"' ~/install.sh && chmod +x install.sh && export LV_BRANCH='release-1.2/neovim-0.8' && yes n | bash ~/install.sh

  echo '创建热链接'
  echo alias ls=lsd >>/data/data/com.termux/files/usr/etc/zshrc && echo alias ls=lsd >>/data/data/com.termux/files/usr/etc/bash.bashrc && echo 'export PATH=/data/data/com.termux/files/home/.local/bin:$PATH' >>/data/data/com.termux/files/home/.zshenv && echo 'export PATH=/data/data/com.termux/files/home/.local/bin:$PATH' >>/data/data/com.termux/files/usr/etc/zshrc && echo 'export PATH=/data/data/com.termux/files/home/.local/bin:$PATH' >>/data/data/com.termux/files/usr/etc/bash.bashrc

  echo '切换pip安装源'
  pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

  echo '切换npm安装源'
  npm install nrm -g
  npm config set registry https://registry.npm.taobao.org

  echo '更新系统设置'
  termux-reload-settings

  echo '切换shell'
  chsh -s fish
  echo 'export PATH=/data/data/com.termux/files/home/.local/bin:$PATH' >>/data/data/com.termux/files/home/.config/fish/config.fish
  curl https://raw.githubusercontent.com/oh-my-fish/oh-my-fish/master/bin/install | fish
  echo -e 'set fish_function_path $fish_function_path "/data/data/com.termux/files/usr/lib/python3.11/site-packages/powerline/bindings/fish"\npowerline-setup' >>/data/data/com.termux/files/home/.config/fish/config.fish
  # && zsh && source ~/.zshrc &&
  pkg install -y vim vim-python
}

install_termux
